<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');
require_once('../shared/confirmation.php');

class Index extends Layout {

    protected $objAlert, $dlgConfirmation;
    protected $dtgService;
    protected $pxyDelete;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/service/menu/index.tpl.php';

        $this->objAlert = new Alert($this);

        $this->dlgConfirmation = new Confirmation($this, null, new QAjaxAction('dlgConfirmation_ConfirmAction'), null);

        $this->pxyDelete = new QControlProxy($this);
        $this->pxyDelete->AddAction(new QClickEvent(), new QAjaxAction('pxyDelete_Click'));
        $this->pxyDelete->AddAction(new QClickEvent(), new QTerminateAction());

        $this->dtgService = new ServiceDataGrid($this);
        $this->dtgService->ShowFilter = false;
        $this->dtgService->ShowFooter = true;

        $this->dtgService->Paginator =
        $this->dtgService->PaginatorAlternate = new QPaginator($this->dtgService);

        $this->dtgService->ItemsPerPage = 10;

        $this->dtgService->MetaAddColumn(QQN::Service()->Name);
        $this->dtgService->MetaAddColumn(QQN::Service()->Order);
        $this->dtgService->AddColumn(new QDataGridColumn('Is Enable', '<?= $_FORM->dtgService_IsEnableColumnRender($_ITEM) ?>', 'HtmlEntities=false', array('OrderByClause' => QQ::OrderBy(QQN::Service()->IsEnable), 'ReverseOrderByClause' => QQ::OrderBy(QQN::Service()->IsEnable, false))));
        $this->dtgService->MetaAddEditLinkColumn('edit.php', 'Edit', 'text-info', null, QMetaControlArgumentType::QueryString);
        $this->dtgService->MetaAddEditProxyColumn($this->pxyDelete, 'Delete', 'text-error', null);
    }

    public function pxyDelete_Click($strFormId, $strControlId, $strParameter) {

        $objService = Service::Load($strParameter);

        $this->dlgConfirmation->strTitle = QApplication::Translate('Delete Service');
        $this->dlgConfirmation->strMessage = QApplication::Translate(sprintf('Do you want to delete "%s"?', $objService->Name));
        $this->dlgConfirmation->btnConfirm->ActionParameter = $objService->Id;
        $this->dlgConfirmation->Show();
    }

    public function dlgConfirmation_ConfirmAction($strFormId, $strControlId, $strParameter) {
        try {
            $objService = Service::Load($strParameter);
            $objService->DeleteAllProjectServices();
            $objService->Delete();

            $this->dtgService->Refresh();
            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate(sprintf('Service "%s" Deleted Successful', $objService->Name)));
        } catch (QCallerException $objExc) {
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }

        $this->dlgConfirmation->Hide();
    }

    public function dtgService_IsEnableColumnRender(Service $objService) {
        if (!$objService->IsEnable)
            return null;

        return '<i class="icon-ok"></i>';
    }
}

Index::Run('Index', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/service/index.tpl.php');
?>